/**
 * @fileOverview 基于cocos2dx-html节点封装的视图基类
 * @author goodzsq@gmail.com
 */

var BaseView = gg.module('view.Base');

var View = BaseView.extend({
    createNode: function(attrs) {
        return cc.Node.create();
    },
    destroyNode: function() {
        this.node.removeFromParent();
    },
    addChild: function(attr) {
        var obj;
        if (attr instanceof View) {
            obj = attr;
            obj.parent = this;
            //@todo remove from old parent
        } else {
            if (!attr.classType) {
                attr.classType = View.classType;
            }
            obj = gg.object(attr, this);
        }
        this.children.push(obj);
        this.node.addChild(obj.node);
    },
    removeChild: function(obj) {
        this.node.removeChild(obj.node);
        for (var i = 0; i < this.children.length; i++) {
            if (node === this.children[i]) {
                this.childrend.splice(i, 1);
                return;
            }
        }
    },
    removeFromParent: function() {
        this.node.removeFromParent();
        if (this.parent) {
            this.parent.removeChild(this);
        }
    }
});

//视图属性
var attr = {};
attr.id = {
    defaultValue: '',
    label: 'ID',
    type: 'datatype.String',
    setter: function(v) {
        if (this.changeId(v)) {
            this.node.id = v;
        }
    },
    getter: function(v) {
        return this.id;
    }
};
attr.visible = {
    defaultValue: true,
    label: '是否可见',
    type: 'datatype.Boolean',
    setter: function(v) {
        if (v) {
            this.node.setVisible(true);
        } else {
            this.node.setVisible(false);
        }
    },
    getter: function() {
        return this.node._visible;
    }
};
attr.x = {
    defaultValue: 0,
    label: 'x',
    type: 'datatype.Integer',
    setter: function(v) {
        this.node.setPositionX(v);
    },
    getter: function() {
        return this.node.getPositionX();
    }
};
attr.y = {
    defaultValue: 0,
    label: 'y',
    type: 'datatype.Integer',
    setter: function(v) {
        this.node.setPositionY(v);
    },
    getter: function() {
        return this.node.getPositionY();
    }
};

View.metadata = _.extend({}, BaseView.metadata, attr);

module.exports = View;